package zdar.screens;

import zdar.Zdar;
import zdar.entities.CardDeck;
import zdar.entities.Unit;
import zdar.entities.UnitTemplate;
import zdar.processing.Ai;
import zdar.processing.CombatProcessor;
import zdar.ui.ActionTable;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Button;
import com.badlogic.gdx.scenes.scene2d.ui.ButtonGroup;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;

public class FightScreen implements Screen{
	Zdar game;
	SpriteBatch batch;
	Skin skin;
	Stage stage;
	OrthographicCamera camera;
	
	Table ui;
	Table combatLog;
	Table playerStatsTable;
	Table enemyStatsTable;
	Table actionTable;
	Button attackButton, actionButton, freeActionButton, endTurnButton;
	ButtonGroup buttongroup;
	
	CombatProcessor combatProcessor;
	CardDeck cardDeck;
	
	Unit playerChar;
	Unit enemyChar;
	Ai enemyAI;
	
	boolean playerActive;
	
	public FightScreen(Zdar game, UnitTemplate playerCharTemplate, UnitTemplate enemyCharTemplate){
		this.game = game;
		
		playerChar = new Unit(playerCharTemplate);
		enemyChar = new Unit(enemyCharTemplate);
		
		batch = new SpriteBatch();
		skin = new Skin(Gdx.files.internal("skins/uiskin.json"));		
		skin.add("default", new BitmapFont());
		
		// Initialize combatty things
		enemyAI = new Ai(enemyChar);
		combatProcessor = new CombatProcessor(playerChar, enemyChar);
		
		// Set the stage!
		stage = new Stage();
		Gdx.input.setInputProcessor(stage);	
		setTables();	
		
		// Create camera!
		camera = new OrthographicCamera();
		camera.setToOrtho(false, 1280, 720);	//TODO move sizes somewhere else		
		
	}
		
	private void setTables(){
		ui = new Table();
		stage.addActor(ui);
		ui.setSize(720, 200);
		ui.setPosition(0, 0);
		ui.left().top();		
		setUiTable();
		
		combatLog = new Table();
		stage.addActor(combatLog);
		combatLog.setSize(560, 200);
		combatLog.setPosition(720, 0);
				
		playerStatsTable = new Table();
		stage.addActor(playerStatsTable);
		playerStatsTable.setSize(640, 520);
		playerStatsTable.setPosition(0, 200);		
		playerStatsTable.left().bottom();
		setPlayerStatsTable();
		System.out.println(playerChar.getDescription());
		
		enemyStatsTable = new Table();
		stage.addActor(enemyStatsTable);
		enemyStatsTable.setSize(640, 520);
		enemyStatsTable.setPosition(640, 200);		
		enemyStatsTable.left().bottom();
		setEnemyStatsTable();
		
		ActionTable temp = new ActionTable(playerChar.getActions(), combatProcessor, skin);
		actionTable = temp.getTable();
		actionTable.setSize(720, 200-40);
		actionTable.setPosition(0, 0);
		
		/*/ Debug
		ui.setDebug(true);
		combatLog.setDebug(true);
		playerStatsTable.setDebug(true);
		enemyStatsTable.setDebug(true);
		actionTable.setDebug(true);
		// */
	}
	
	// TODO disable attack buttons until turn is over
	
	
	private void setUiTable(){
		attackButton = new TextButton("Attack", skin, "toggle");
		actionButton = new TextButton("Primary Action", skin, "toggle");
		freeActionButton = new TextButton("Free Action", skin, "toggle");
		endTurnButton = new TextButton("End Turn", skin);
		
		buttongroup = new ButtonGroup(attackButton, actionButton, freeActionButton);
		buttongroup.setMaxCheckCount(1);
		buttongroup.setMinCheckCount(0);
		buttongroup.setUncheckLast(true);		
		
		attackButton.addListener(new ChangeListener(){
			public void changed(ChangeEvent event, Actor actor) {
				//enemyCharTable.remove();
				//stage.addActor(playerCharTable);
				// Add attack table based on player Chars meleeAttack and rangeAttack lists.
				System.out.println("You prepare to attack your opponent!");
				stage.addActor(actionTable);
			}			
		});
		
		actionButton.addListener(new ChangeListener(){
			public void changed(ChangeEvent event, Actor actor) {
				// List non attack options for the player, of which there are none (for now).
				System.out.println("You yell hurtful obsceneties at your opponent.");
			}			
		});
		
		freeActionButton.addListener(new ChangeListener(){
			public void changed(ChangeEvent event, Actor actor) {
				// List free actions for the player, of which there are none (for now).
				System.out.println("You hurl obsceneties at your opponent. Though they cause no outward reaction they still hurt on the inside.");
			}			
		});
		
		endTurnButton.addListener(new ChangeListener(){
			public void changed(ChangeEvent event, Actor actor) {
				// reset button toggles
				buttongroup.uncheckAll();
				combatProcessor.endTurn();
				System.out.println("Your opponent will eventually do something, but for now it will just stand there.");
			}			
		});		
		
		
		ui.add(attackButton).width(180).height(30);
		ui.add(actionButton).width(180).height(30);
		ui.add(freeActionButton).width(180).height(30);
		ui.add(endTurnButton).width(180).height(30);
		ui.row();
		ui.add(new Label("==============================================================================", skin)).colspan(4).center().height(10);
	}
	private void setPlayerStatsTable(){
		playerStatsTable.add(new Label("Name: ", skin)).left();
		playerStatsTable.add(playerChar.charSheetDisplay.getName()).left();
		playerStatsTable.row();
		playerStatsTable.add(new Label("Description: ", skin)).left();
		playerStatsTable.add(playerChar.charSheetDisplay.getDescription()).left();
		playerStatsTable.row();
		playerStatsTable.add(new Label("Wounds: ", skin)).left();
		playerStatsTable.add(playerChar.charSheetDisplay.getWounds()).left();
		playerStatsTable.row();
		playerStatsTable.add(new Label("Fatigue: ", skin)).left();
		playerStatsTable.add(playerChar.charSheetDisplay.getFatigue()).left();
	}
	private void setEnemyStatsTable(){
		enemyStatsTable.add(new Label("Name: ", skin)).left();
		enemyStatsTable.add(enemyChar.charSheetDisplay.getName()).left();
		enemyStatsTable.row();
		enemyStatsTable.add(new Label("Description: ", skin)).left();
		enemyStatsTable.add(enemyChar.charSheetDisplay.getDescription()).left();
		enemyStatsTable.row();
		enemyStatsTable.add(new Label("Wounds: ", skin)).left();
		enemyStatsTable.add(enemyChar.charSheetDisplay.getWounds()).left();
		enemyStatsTable.row();
		enemyStatsTable.add(new Label("Fatigue: ", skin)).left();
		enemyStatsTable.add(enemyChar.charSheetDisplay.getFatigue()).left();
	}
	
	private void update(){
		playerActive = combatProcessor.playerTurn();
		if (!playerActive){
			enemyAI.processTurn(combatProcessor);
		}
		
		// Do things to buttons if it's player's turn
	}
	
	// ==============================Auto=gen=methods============================================================================
	@Override
	public void render(float delta) {
		update();
		
		// Clear the screen with some tasty dark blue
		Gdx.gl.glClearColor(0, 0, 0.2f, 1);
		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
		stage.act();
		stage.draw();	
		
		// tell camera to get its shit together and update its matrices
		camera.update();
		
		// tell the sprite to render in coord system set by camera
		game.batch.setProjectionMatrix(camera.combined);
		
		// begin a new batch and draw things again
		game.batch.begin();
		//game.font.draw(game.batch, "Let's fighting!", 1150, 720);
		//game.batch.draw(someShit, some, where);
		game.batch.end();
		
	}

	@Override
	public void resize(int width, int height) {
		// stage.getViewport().update(width, height, true);	//We don't even support resizing right now		
	}

	@Override
	public void show() {
	}

	@Override
	public void hide() {
	}

	@Override
	public void pause() {
	}

	@Override
	public void resume() {
	}

	@Override
	public void dispose() {
		// dispose images
		// dispose sounds
		// I guess that's all
		stage.dispose();
		skin.dispose();		
	}

}
